package com.example.springboot.controller;

import com.example.springboot.entity.MerchantOrder;
import com.example.springboot.service.MerchantOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/merchant-orders")
public class MerchantOrderController {

    @Autowired
    private MerchantOrderService merchantOrderService;

    @GetMapping
    public ResponseEntity<Map<String, Object>> findAll(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String orderId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String serviceType,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        // 传递所有参数给 service 方法
        List<MerchantOrder> orders = merchantOrderService.findAll(page, size, role, username, orderId, status, serviceType);
        int totalOrders = merchantOrderService.getTotalOrders(role, username, orderId, status, serviceType);

        Map<String, Object> response = new HashMap<>();
        response.put("data", orders);
        response.put("total", totalOrders);

        return ResponseEntity.ok(response);
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<MerchantOrder> findById(
            @PathVariable Integer orderId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        MerchantOrder order = merchantOrderService.findById(orderId, role, username);
        return ResponseEntity.ok(order);
    }

    @PostMapping
    public ResponseEntity<MerchantOrder> insert(
            @RequestBody MerchantOrder merchantOrder,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        MerchantOrder createdOrder = merchantOrderService.insert(merchantOrder, role, username);
        return ResponseEntity.ok(createdOrder);
    }

    @PutMapping("/{orderId}")
    public ResponseEntity<MerchantOrder> update(
            @PathVariable Integer orderId,
            @RequestBody MerchantOrder merchantOrder,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        merchantOrder.setOrderId(orderId);
        MerchantOrder updatedOrder = merchantOrderService.update(merchantOrder, role, username);
        return ResponseEntity.ok(updatedOrder);
    }

    @DeleteMapping("/{orderId}")
    public ResponseEntity<Void> deleteById(
            @PathVariable Integer orderId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        merchantOrderService.deleteById(orderId, role, username);
        return ResponseEntity.noContent().build();
    }
}